package com.hs.utils;

/*
 * Created on 2005-6-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */


import java.util.regex.*;

/**
 * 对字符串进行进行正则验证
 * 
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class Verify{
    // 是否emial
    public static boolean isEmail( String str ) {
        String p = "^\\w+(\\.\\w+){0,2}@\\w+(\\.\\w+)*\\.[a-zA-Z]{2,3}$";
        return verify(str, p);
    }

    // 是否
    public static boolean isDate( String str ) {
        // 将 "/" 转换为 "-"
        String v = str.replaceAll("/", "-");

        // 将 mm-dd-yyyy 转换为 yy-mm-dd
        String p = "^(0{0,1}[1-9]{1}|1[0-2]{1})-(0{0,1}[1-9]{1}|([12][0-9]){1}|30|31)-([1-2][0-9]{3})$";
        if( verify(v, p) ) {
            String[] ss = v.split("-");
            v = ss[2] + "-" + ss[0] + "-" + ss[1];
        }

        return isDateS(v);
    }

    // 是否 yyyy-mm-dd 格式的日期
    public static boolean isDateS( String str ) {
        String p = "";
        // 1,3,5,7,8 10 月，可以有31天
        p = "^([1-2][0-9]{3})-(0{0,1}[13578]|1[02])-"
                + "(0{0,1}[1-9]|[1-2]{0,1}[0-9]|3[0-1]{1})$";
        if( verify(str, p) ) {
            return true;
        }

        // 4,6,9,11 月，最多30天
        p = "^[1-2][0-9]{3}-(0{0,1}[469]|11)-(0{0,1}[1-9]|[1-2]{0,1}[0-9]|30)$";
        if( verify(str, p) ) {
            return true;
        }

        // 2 月，最多28天时为有效
        p = "^[1-2][0-9]{3}-(0{0,1}2)-(0{0,1}[1-9]|[1-2]{0,1}[0-8])$";
        if( verify(str, p) ) {
            return true;
        }

        // 2 月 29
        p = "^[1-2][0-9]{3}-(0{0,1}2)-29$";
        if( verify(str, p) ) {
            p = str.split("-")[0];
            int y = Integer.parseInt(p);
            if( (0 == y % 4) && (0 != y % 400) ) {
                return true;
            } else {
                return false;
            }
        }

        return false;
    }

    // 是否时间，从 0:0:0 到 23:59:59
    public static boolean isTime( String str ) {
        String p = "^([01]{0,1}[0-9]|2[0-3])(:[0-5]?[0-9]){1,2}$";

        return verify(str, p);
    }

    // 是否 ip，格式 xxx.xxx.xxx.xxx
    public static boolean isIp( String str ) {
        String p = "^(([01]{0,1}[0-9]{1,2})|(2([0-4]{1}[0-9]{1}|5[0-5]{1})))(\\.(([01]{0,1}[0-9]{1,2})|(2([0-4]{1}[0-9]{1}|5[0-5]{1})))){3}$";

        return verify(str, p);
    }

    //  是否邮政编码，由六个数字组成，例：100011
    public static boolean isPostcode( String value ) {
        String p = "^[0-9]{6}$";
        return verify(value, p);
    }

    //  是否 Double
    public static boolean isDouble( String value ) {
        return isFloat(value);
    }

    //     是否浮点数
    public static boolean isFloat( String value ) {
        String p = "^(-{0,1}|\\+{0,1})[0-9]+(\\.{0,1}[0-9]+)$";
        return verify(value, p);
    }

    //     是否一个整数
    public static boolean isInt( String value ) {
        String p = "^[\\+-]?[0-9]{1,}$";
        return verify(value, p);
    }

    public static boolean isUrl( String value ) {
        String p = "((\\w+):\\/\\/)?[\\w-]+(\\.[\\w-]+)*\\.[a-zA-Z]{2,3}(:\\d*)?(\\/[^# ]*)*";
        return verify(value, p);
    }

    //  是否全中文字符
    public static boolean isAllChinese( String value ) {
        String p = "^[\\u4e00-\\u9fa5]+$";
        return verify(value, p);
    }

    //     是否全中文字符
    public static boolean isChineseIncluded( String value ) {
        String p = "[\\u4e00-\\u9fa5]+";
        return verify(value, p);
    }

    //     是否双字节字符
    public static boolean isAllDoubleChar( String value ) {
        String p = "^[^\\x00-\\xff]+$";
        return verify(value, p);
    }

    //     是否双字节字符
    public static boolean isDoubleCharIncluded( String value ) {
        String p = "[^\\x00-\\xff]+";
        return verify(value, p);
    }

    // 正则表达式校验
    public static boolean verify( String str, String pattern ) {
        Pattern p = null; //正则表达式
        Matcher m = null; //操作的字符串

        p = Pattern.compile(pattern);
        m = p.matcher(str);


        return m.matches();
    }
}
